<?php
/**
 * The Shoehorn Project
 * 
 * The shoehorn project is designed to make managing, creating, and modifying 
 * scripts as easy as possible.
 * 
 * @version 0.1
 * @package shoehorn
 * @copyright 2008
 * @author Christopher "OneSeventeen" Adams
 * 
 */


/**
 * Core Shoehorn Class
 *
 * @package shoehorn
 * @subpackage classes
 * 
 */

class shoehorn {

	public $auth;
	public $db;
	public $err;
	
	/**
	 * Shoehorn Class Construct
	 * 
	 * Establishes Authentication object and logs in if needed
	 * Also establishes database object
	 *
	 */
	function __construct()
	{
		//establish error object
		$this->err = new Shoehorn_Error();
		//or die("error: no error object");  //maybe make this abstract later?
		
		//establish the authentication object
		$this->auth = Shoehorn_Auth::getInstance(SHOEHORN_AUTH_TYPE, $this->err); 
		//or die("could not load authentication");
		
		//Check for a login form submission, if so try to log in
		$this->logInIfNeeded();
		
		//Check for logout request, and if so log out
		if(isset($_REQUEST['logout'])) {
			$this->auth->logout();
		}
		
		//establish the database object and connect to database
		$this->db = Shoehorn_Data::getInstance(SHOEHORN_DB_DSN, $this->err); 
		//or die("error: could not connect to database");
	}
	
	/**
	 * Check to see if the login form was submitted, and if so, log the user in
	 * using the authentication object's login method.
	 * 
	 * Relies on POST variables submitting username, password, and 
	 * loginform='login'
	 *
	 * @return true|false
	 */
	private function logInIfNeeded()
	{
		$lr = true;
		if(isset($_POST['username']) && isset($_POST['password']) 
		&& isset($_POST['loginform']) && $_POST['loginform'] == 'login') {
			$lr = $this->auth->login($_POST['username'], $_POST['password']);
		}
		if(!$lr) {
			print_r($this->auth->errors);
		}
		return($lr);
	}
	

	
}